package com.yuexin.project.store.goods.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuexin.common.utils.security.ShiroUtils;
import com.yuexin.framework.aspectj.lang.annotation.Log;
import com.yuexin.framework.aspectj.lang.enums.BusinessType;
import com.yuexin.framework.web.controller.BaseController;
import com.yuexin.framework.web.domain.AjaxResult;
import com.yuexin.framework.web.page.TableDataInfo;
import com.yuexin.project.store.goods.domain.GoodsList;
import com.yuexin.project.store.goods.mapper.GoodsListMapper;
import com.yuexin.project.store.goods.service.IGoodsListService;
import com.yuexin.project.store.sort.service.IGoodsSortService;

/**
 * 物品管理 信息操作处理
 * 
 * @author yuexin
 * @date 2018-11-29
 */
@Controller
@RequestMapping("/store/goodsList")
public class GoodsListController extends BaseController
{
    private String prefix = "store/goodsList";
	
	@Autowired
	private IGoodsListService goodsListService;
	
	@Autowired
	private IGoodsSortService goodsSortService;
	
	@Autowired
	private GoodsListMapper goodsListMapper;
	
	@RequiresPermissions("store:goodsList:view")
	@GetMapping()
	public String goodsList()
	{
	    return prefix + "/goodsList";
	}
	
	/**
	 * 查询物品管理列表
	 */
	@RequiresPermissions("store:goodsList:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(GoodsList goodsList)
	{
		startPage();
		goodsList.setCompanyId(ShiroUtils.getCompanyId());
        List<GoodsList> list = goodsListService.selectGoodsListList(goodsList);
		return getDataTable(list);
	}
	
	/**
	 * 新增物品管理
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		mmap.put("goodsSorts", goodsSortService.selectGoodsSortAll(ShiroUtils.getCompanyId()));
	    return prefix + "/add";
	}
	
	/**
	  * 校验名称
    */
   @PostMapping("/checkNameUnique")
   @ResponseBody
   public String checkNameUnique(GoodsList goodsList)
   {
   	int names = goodsListMapper.checkName(goodsList.getGoodsName());
		if (names > 0)
       {
			return "1";
       }
       return "0";
   }
   
	   /**
		  * 校验名称
	  */
	 @PostMapping("/checkNumberUnique")
	 @ResponseBody
	 public String checkNumberUnique(GoodsList goodsList)
	 {
	 	int names = goodsListMapper.checkNumber(goodsList.getGoodsNumber());
			if (names > 0)
	     {
				return "1";
	     }
	     return "0";
	 }
   
	/**
	 * 新增保存物品管理
	 */
	@RequiresPermissions("store:goodsList:add")
	@Log(title = "物品管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(GoodsList goodsList)
	{		
		goodsList.setCompanyId(ShiroUtils.getCompanyId());
		return toAjax(goodsListService.insertGoodsList(goodsList));
	}

	/**
	 * 修改物品管理
	 */
	@GetMapping("/edit/{goodsId}")
	public String edit(@PathVariable("goodsId") Integer goodsId, ModelMap mmap)
	{
		GoodsList goodsList = goodsListService.selectGoodsListById(goodsId);
		mmap.put("goodsList", goodsList);
		mmap.put("goodsSorts", goodsSortService.selectGoodsSortAll(ShiroUtils.getCompanyId()));
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存物品管理
	 */
	@RequiresPermissions("store:goodsList:edit")
	@Log(title = "物品管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(GoodsList goodsList)
	{		
		return toAjax(goodsListService.updateGoodsList(goodsList));
	}
	
	/**
	 * 删除物品管理
	 */
	@RequiresPermissions("store:goodsList:remove")
	@Log(title = "物品管理", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(goodsListService.deleteGoodsListByIds(ids));
	}
	
}
